package com.study.talk.ui.adapter

import android.graphics.Color
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.google.android.material.imageview.ShapeableImageView
import com.google.android.material.shape.ShapeAppearanceModel
import com.study.common.MDParser
import com.study.talk.databinding.AnalyzePictureListItemBinding
import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionContentPart
import com.volcengine.ark.runtime.model.completion.chat.ChatMessage
import com.volcengine.ark.runtime.model.completion.chat.ChatMessageRole

class AnalyzePictureListAdapter(private val data: List<ChatMessage>) :
    RecyclerView.Adapter<AnalyzePictureListAdapter.ViewHolder>() {

    class ViewHolder(val bing: AnalyzePictureListItemBinding) : RecyclerView.ViewHolder(bing.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            AnalyzePictureListItemBinding.inflate(
                LayoutInflater.from(parent.context), parent, false
            )
        )
    }

    override fun getItemCount(): Int {
        return data.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val message = data[position]
        if (message.role == ChatMessageRole.USER) {
            holder.bing.analyzePictureListItemContentLeft.visibility = View.GONE
            holder.bing.analyzePictureListItemContentRight.visibility = View.VISIBLE
            render(holder, message)
        } else {
            holder.bing.analyzePictureListItemContentRight.visibility = View.GONE
            holder.bing.analyzePictureListItemContentLeft.visibility = View.VISIBLE
            holder.bing.analyzePictureListItemContentLeft.text =
                MDParser.toSpanned(message.content.toString())
        }
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int, payloads: MutableList<Any>) {
        if (payloads.isNotEmpty()) {
            for (payload in payloads) {
                when (payload) {
                    "contentUpdating" -> {
                        val message = data[position]
                        if (message.role == ChatMessageRole.USER) {
                            render(holder, message)
                        } else {
                            holder.bing.analyzePictureListItemContentLeft.text =
                                MDParser.toSpanned(message.content.toString())
                        }
                    }
                }
            }
        } else {
            super.onBindViewHolder(holder, position, payloads)
        }
    }

    private fun render(holder: ViewHolder, message: ChatMessage) {
        if (message.content is List<*>) { //图片识别的内容
            (message.content as List<*>).forEach {
                if (it is ChatCompletionContentPart) {
                    when (it.type) {
                        "image_url" -> {
                            val shapeImageView = createImageView(holder)
                            holder.bing.analyzePictureListItemContentRight.addView(shapeImageView)
                            Glide.with(holder.bing.root).load(it.imageUrl.url).into(shapeImageView)
                        }

                        "text" -> {
                            val textView = createTextView(holder)
                            holder.bing.analyzePictureListItemContentRight.addView(textView)
                            textView.text = it.text
                        }
                    }
                }
            }
        } else {
            val textView = createTextView(holder)
            holder.bing.analyzePictureListItemContentRight.addView(textView)
            textView.text = message.content.toString()
        }
    }

    private fun createImageView(holder: ViewHolder): ShapeableImageView {
        val view =
            ShapeableImageView(holder.bing.analyzePictureListItemContentRight.context).apply {
                adjustViewBounds = true
                layoutParams = LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT
                )
                shapeAppearanceModel = ShapeAppearanceModel.builder().setAllCornerSizes(
                    holder.bing.root.context.resources.displayMetrics.density * 10
                ).build()
            }
        return view
    }

    private fun createTextView(holder: ViewHolder): TextView {
        val view = TextView(holder.bing.analyzePictureListItemContentRight.context).apply {
            isSelected = true
            layoutParams = LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT
            )
            gravity = Gravity.CENTER_VERTICAL
            setTextColor(Color.WHITE)
            textSize = 16f
        }
        return view
    }
}